import 'dart:ui';

import 'package:beng_beng_utils/utils/log/BBLogUtils.dart';

import '../ToastUtils.dart';
import '../log/BBLogType.dart';

extension DurtionExt on Duration {
  String bbJoinTime() {
    String twoDigits(int n) {
      if (n >= 10) return "$n";
      return "0$n";
    }

    String twoDigitMinutes = twoDigits(this.inMinutes.remainder(60));
    String twoDigitSeconds = twoDigits(this.inSeconds.remainder(60));
    if (this.inHours > 0) {
      return "${this.inHours}:${twoDigitMinutes}:${twoDigitSeconds}";
    } else {
      return "${twoDigitMinutes}:${twoDigitSeconds}";
    }
  }
}

extension StringExt on String {
  void bbToast() {
    showToastMsg(this);
  }

  void bbLogE(
      {String tag = "fastLog",
      BBLogFrom from = BBLogFrom.fastLog,
      bool isSave = false}) {
    BBLogUtils.e(this, tag: tag, from: from, isSave: isSave);
  }

  void bbLogW(
      {String tag = "fastLog",
      BBLogFrom from = BBLogFrom.fastLog,
      bool isSave = false}) {
    BBLogUtils.w(this, tag: tag, from: from, isSave: isSave);
  }

  void bbLogI(
      {String tag = "fastLog",
      BBLogFrom from = BBLogFrom.fastLog,
      bool isSave = false}) {
    BBLogUtils.i(this, tag: tag, from: from, isSave: isSave);
  }

  void bbLogD(
      {String tag = "fastLog",
      BBLogFrom from = BBLogFrom.fastLog,
      bool isSave = false}) {
    BBLogUtils.d(this, tag: tag, from: from, isSave: isSave);
  }

  //字符串转换为颜色
  Color toDealColor() {
    // 计算哈希值
    int hash = this.hashCode;

    // 提取红、绿、蓝分量
    int red = (hash & 0xFF0000) >> 16;
    int green = (hash & 0xFF00) >> 8;
    int blue = hash & 0xFF;

    // 构建颜色字符串
    String color =
        '#${red.toRadixString(16)}${green.toRadixString(16)}${blue.toRadixString(16)}';

    return Color(int.parse(color.replaceAll("#", "0xFF")));
  }

  /// 信息太长,分段打印
  void printBySplit({String tag = ""}) {
    String msg = this;
    //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
    //  把4*1024的MAX字节打印长度改为1000字符数
    int maxStrLength = 1000;
    //大于1000时
    print(
        "╔══════════════════════════════════════════分段打印${tag}开始═════════════════════════════════════════════════════════");

    while (msg.length > maxStrLength) {
      print(msg.substring(0, maxStrLength));
      msg = msg.substring(maxStrLength);
    }
    //剩余部分
    print(msg);
    print(
        "╚══════════════════════════════════════════分段打印${tag}结束═════════════════════════════════════════════════════════");
  }
}
